package dell.yangzhou;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class DemoLambda03 {
    public static void main(String[] args) {
        System.out.println(" ====情况一：对象：：实例方法\n=======");
        /* * Consumer中的void accept（T t）\n" +
            PrintStream中的void println（T t）*/
        Consumer<String>con1=s -> System.out.println(s);
        //lambda表达式
        con1.accept("北京");
        System.out.println("=================");
        PrintStream ps=System.out;
        Consumer<String>con2=ps::println;
        //使用方法引用
        con2.accept("beijing");
       /* * Supplier中的T get（）
        * Employee中的String getName（）*/
    Employee emp=new Employee(1001,"Tom",23,5600);
        Supplier<String>sup1=()->emp.getName();
        //lambda表达式无参数却有返回值
        System.out.println(sup1.get());
        System.out.println("==============");
        //【注意与上面lambda表达式对比着看】
        Supplier<String>sup2=emp::getName;
        System.out.println(sup2.get());
        System.out.println("======情况二：类：：静态方法========");
       /* Comparator中的int compare（T t1，T t2）
        Integer中的int compare（T t1，T t2）
        * */
        Comparator<Integer>com1=(t1,t2)->Integer.compare(t1,t2);
        //lambda表达式的写法
        System.out.println(com1.compare(12,34));
        System.out.println("=============");
        //方法引用的写法
        Comparator<Integer>com2=Integer::compare;
        //compare是静态方法
        System.out.println(com2.compare(34,12));
        /**/
        Function<Double,Long>func1=d->Math.round(d);
        System.out.println(func1.apply(12.3));
        System.out.println("=================");
        Function<Double,Long>func2=Math::round;
        System.out.println(func2.apply(12.6));
        System.out.println("======情况三：类：：静态方法========");
        /*Comparator中的int compare（T t1，T t2）
        * String中的int t1.compareTo（t2）*/
        Comparator<String> comt1=(s1,s2)->s1.compareTo(s2);
        //s1作为对象，返回s2
        System.out.println(comt1.compare("abc","adc"));
        System.out.println("==========");
        Comparator<String> comt2=String::compareTo;
        System.out.println(comt2.compare("def","ddf"));
        /*BiPredicate中的boolean test（T t1，T t2）
        * String中的boolean t1.equals（t2）*/
        BiPredicate<String,String>pre1=(s1,s2)->s1.equals(s2);
        System.out.println(pre1.test("abc","abc"));
        System.out.println("=============");
        BiPredicate<String,String>pre2=String::equals;
        System.out.println(pre2.test("abd","abc"));
        /*Function中的R apply（T t）
        * Employee中的String getName（）*/
        Employee employee=new Employee(1001,"Jerry",12,500);
        Function<Employee,String>fun1=e->e.getName();
        System.out.println(fun1.apply(employee));
        System.out.println("===========");
        Function<Employee,String>fun2=Employee::getName;
        System.out.println(fun2.apply(employee));

    }
}
